<script setup>

import {getCurrentInstance, ref} from "vue";
import {deleteSysRole, listSysRole} from "@/api/system/role";
import RoleForm from "@/views/system/role/RoleForm.vue";
import AssignRoleForm from "@/views/system/role/AssignRoleForm.vue";

const {proxy} = getCurrentInstance();
const roleList = ref([]);
const total = ref(0);
const roleFormRef = ref();
const queryFormRef = ref();
const assignRoleFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  roleName: null,
  timeArr: null
})
const selectIdIndex = ref(-1);
const selectId = ref();

const getList = async () => {
  const {data} = await listSysRole(queryForm.value)
  total.value = data.total;
  roleList.value = data.list;
}

const openRole = (roleId, type) => {
  roleFormRef.value.open(roleId, type)
}

const deleteRole = async (roleId) => {
  proxy.$modal.confirm('确定删除该角色吗？').then(async () => {
    await deleteSysRole(roleId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (index, row) => {
  selectIdIndex.value = index;
  selectId.value = row.id
}

const handleUpdate = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  openRole(selectId.value, 'update');
}

const handleDelete = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  deleteRole(selectId.value)
}

const handleAssignRole = (roleId, type) => {
  assignRoleFormRef.value.open(roleId, type)
}

getList();
</script>

<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryFormRef" :inline="true" class="demo-form-inline">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="queryForm.roleName" placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="timeArr">
        <el-date-picker
            v-model="queryForm.timeArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </el-button>
        <el-button plain @click="handleReset">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="flex mb-5">
      <el-button size="default" type="primary" plain class="flex items-center" @click="openRole(null, 'create')" v-hasPermission="['system:role:add']">
        <icon icon="ep-plus" size="14px" color="unset" class="mr-1"/>
        新增
      </el-button>

      <el-button size="default" type="success" plain class="flex items-center" @click="handleUpdate" v-hasPermission="['system:menu:edit']">
        <icon icon="ep-edit" size="14px" color="unset" class="mr-1"/>
        编辑
      </el-button>

      <el-button size="default" type="danger" plain class="flex items-center" @click="handleDelete" v-hasPermission="['system:menu:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </el-button>
    </div>

    <el-table :data="roleList" :border="true" size="large" highlight-current-row>
      <el-table-column width="55">
        <template #default="scope">
          <el-radio v-model="selectIdIndex" :value="scope.$index" :disabled="scope.row.id === 1"
                    @change="handleSelectionChange(scope.$index,scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="角色ID"/>
      <el-table-column prop="roleCode" label="角色编码"/>
      <el-table-column prop="roleName" label="角色名称"/>
      <el-table-column fixed="right" label="操作" width="200px">
        <template #default="scope">
          <div v-if="scope.row.id !== 1">
            <el-button class="ml-2" type="primary" link @click="openRole(scope.row.id,'update')" v-hasPermission="['system:menu:edit']">编辑
            </el-button>
            <el-button class="ml-2" type="danger" link @click="deleteRole(scope.row.id)" v-hasPermission="['system:menu:delete']">
              删除
            </el-button>
            <el-button class="ml-2" type="warning" link @click="handleAssignRole(scope.row.id,'create')" v-hasPermission="['system:menu:assing_roles']">分配权限
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

    <role-form ref="roleFormRef" @success="getList"/>
    <assign-role-form ref="assignRoleFormRef" @success="getList"/>
  </div>
</template>

<style scoped lang="scss">

</style>
